<template>
  <div id="home">
    <!-- 顶部购物街div -->
    <nav-bar class="nav-color">
      <div slot="center">购物街</div>
    </nav-bar>

    <tab-control
      class="tab-control"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isFiexed"
    ></tab-control>

    <!-- 把轮播图组件封装到子组件中 .v-bind:绑定bannners值-->
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
      @pullingUp="contentPullUp"
    >
      <!-- 轮播图组件 -->
      <home-swiper :banners="banners" @swpierImage="swpierImage"></home-swiper>
      <!-- 推荐界面组件 -->
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>

    <back-top @click.native="backClick" v-show="isBackTopShow"> </back-top>
  </div>
</template>

<script>
// 导入组件
import NavBar from "@/components/common/navbar/NavBar.vue"; //导入导航条组
import TabControl from "@/components/content/tabControl/TarControl.vue";
import GoodsList from "@/components/content/goods/GoodsList.vue";
import Scroll from "@/components/common/scroll/Scroll.vue";
import BackTop from "@/components/content/backtop/BackTop.vue";
// 导入方法
import { getHomeMultidata, getHomeData } from "@/api/home/home"; //导入获得数据的api接口方法

import { debounce } from "../../common/utils";

// 导入公共组件
import HomeSwiper from "./childComps/HomeSwiper.vue"; //导入home界面的轮播图组件
import RecommendView from "./childComps/RecommendView.vue"; //导入推荐icon组件
import FeatureView from "./childComps/FeatureView.vue"; //导入本周流行组件

export default {
  name: "Home",
  // 注册组件
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      result: null /* 结果信息保存实体 */,
      banners: [] /* 轮播图数据存放列表 */,
      recommends: [] /* 推荐图片存放列表 */,
      // 请求到的数据模型
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop" /* 设置默认的title标题 */,
      isBackTopShow: false /* 设置默认backTop按钮实现样式 */,
      taboffsetTop: 0 /* 顶部高度 */,
      isFiexed: false /* 默认吸顶效果为false */,
    };
  },
  created() {
    /* 1.请求商品数据 */
    this.getHomeGoods("pop");
    this.getHomeGoods("sell");
    this.getHomeGoods("new");

    /* 2.请求多个数据 */
    this.getHomeMultidata();
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },

  mounted() {
    // 防抖函数的调用
    const refresh = debounce(this.$refs.scroll.refresh, 200); //用一个变量存放函数的返回值
    /* 3.监听图片加载完成 */
    this.$bus.$on("itemImageLoad", () => {
      refresh();
    });
  },

  methods: {
    /* 
    1.网络请求方法
    */
    // 请求多个数据
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // console.log(res); //res指的是一整个大的对象
        // this.result = res; //保存获取的数据，指向res的的地址，然后释放res变量
        this.recommends = res.data.recommend.list;
        this.banners = res.data.banner.list;
      });
    },
    // 请求首页数据api方法
    getHomeGoods(type) {
      const page = this.goods[type].page + 1; //定义一个用于存放页码的变量
      getHomeData(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list); //获得请求到的数据，用ES6的拓展运算符push进list列表
        this.goods[type].page += 1; //页码数+1
        this.$refs.scroll.finishPullUp(); //完成加载更多操作
      });
    },

    /* 
    2.事件监听方法
    */
    // 点击顶部导航栏切换数据，获得当前title的索引值
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },

    // 返回顶部功能实现
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },

    // 控制backtop图片是否显示,如果小于-1000则显示img
    contentScroll(posistion) {
      // 判断BackTop是否显示
      this.isBackTopShow = posistion.y < -1000;
      // 判断吸顶效果是否实现
      this.isFiexed =
        -posistion.y >
        this.taboffsetTop; /* 如果超过了当前的offsetTop高度,则设置属性为true*/
    },

    // 点击加载更多方法实现
    contentPullUp() {
      this.getHomeGoods(this.currentType);
      this.$refs.scroll.scroll.refresh(); /* 刷新界面数据 */
    },

    swpierImage() {
      // 吸顶效果的实现
      // 所有的组件都有一个属性$el,用于获取组件中的元素
      // this.taboffsetTop = this.$refs.tabControl.offsetTop;
      this.taboffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
  },
};
</script>

<style scoped>
.nav-color {
  background-color: var(--color-tint);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9;
}
#home {
  padding-top: 44px;
  height: 100vh;
  position: relative;
  /* viewport hight:可见高度
   */
}
/* 设置title样式，当移动到某一处位置时，固定不动 */
.tab-control {
  position: relative; /* 粘性布局 */
  /* top: 44px; */
  z-index: 9;
}

/* 给content设置高度,子绝父相定位 */
.content {
  /* height: calc(100% - 93px); 44px(顶部导航栏的高度) + 49px(下方功能键)  */
  overflow: hidden;
  /* margin-top: 44px; */
  position: absolute;
  left: 0;
  right: 0;
  top: 44px;
  bottom: 49px;
}
</style>